package cn.com.pxto.commons.propertiesutil;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * @author prewind
 * Created on 2004-8-11
 */
public class Configuration {
	private Properties config = new Properties();//����
	private String fn = "globalconf.properties";//�����ļ���,��Ĭ��ֵ
	private static Log log = LogFactory.getLog(Configuration.class);
	public Configuration() {
	}
	/**
	 * ���ָ�����ļ�����һ������
	 * @param fileName
	 * @throws ConfigurationException
	 */
	public Configuration(String fileName) throws ConfigurationException {
		try {
			InputStream in = getClass().getResourceAsStream(fileName);
			config.load(in); //�����ļ�
			in.close();
		} catch (IOException ex) {
			throw new ConfigurationException("Can't read files:" + fileName);
		}
		fn = fileName;
	}
	/**
	 * ָ����������ƣ���������ֵ
	 * @param itemName String ���������
	 * @return String ����ֵ
	 */
	public String getValue(String itemName) {
		return config.getProperty(itemName);
	}

	/**
	 * ָ����������ƺ�Ĭ��ֵ����������ֵ
	 * @param itemName String ���������
	 * @param defaultValue String Ĭ������ֵ
	 * @return String ����ֵ
	 */
	public String getValue(String itemName, String defaultValue) {
		String result = config.getProperty(itemName, defaultValue);
		if (log.isDebugEnabled()){
			log.debug("getproperty values is:"+result);
		}
		return result;
	}

	/**
	 * ������������Ƽ���ֵ
	 * @param itemName String,��������
	 * @param value String,����ֵ
	 */
	public void setValue(String itemName, String value) {
		config.setProperty(itemName, value);
		return;
	}

	/**
	 * ���������ļ���ָ���ļ����̧ͷ����
	 * @param fileName String,�������õ��ļ���
	 * @param description String,̧ͷ����
	 * @throws ConfigurationException �ļ��޷�����
	 */
	public void saveFile(String fileName, String description)
			throws ConfigurationException {
		try {
			FileOutputStream fout = new FileOutputStream(fileName);
			config.store(fout, description);//�����ļ�
			fout.close();
		} catch (IOException ex) {
			throw new ConfigurationException("Can't not save files:" + fileName);
		}
	}
	/**
	 * ���������ļ���ָ���ļ���
	 * @param fileName String,�������õ��ļ���
	 * @throws ConfigurationException �ļ��޷�����
	 */
	public void saveFile(String fileName) throws ConfigurationException {
		saveFile(fileName, "");
	}
	/**
	 *���������ļ�������ԭ�ļ�����ȡ�ļ�ʱ������¼�ļ���ȡ��4Դ��
	 * @throws ConfigurationException ����ָ��һ���ļ�����
	 */
	public void saveFile() throws ConfigurationException {
		if (fn.length() == 0)
			throw new ConfigurationException("Must spification a file to save");
		saveFile(fn);
	}
}